package com.elcl.activity;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.elcl.andbaselibrary.R;
import com.elcl.storage.ApplicationCache;
import com.elcl.util.StringUtils;


/**
 * Created by lichunlin on 16/6/30.
 * 顶部的title
 */
public class PageTopView extends FrameLayout {

    private LinearLayout layout;
    private TextView txt_middle, txt_pt_right;
    private ImageView img_right;
    private ImageView img_back;

    Context context;

    public PageTopView(Context context) {
        super(context);
    }

    public PageTopView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PageTopViews);
        String title = (String) ta.getText(R.styleable.PageTopViews_title);
        String rightTxt = (String) ta.getText(R.styleable.PageTopViews_rightTxt);
        Integer rightRes = ta.getInteger(R.styleable.PageTopViews_rightRes, 0);
        ta.recycle();

        initView(context, title, rightRes, rightTxt);
    }

    public PageTopView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 初始化头条
     *
     * @param context
     */
    private void initView(Context context, String title, Integer rightRes, String rightTxt) {
        this.context = context;
        layout = (LinearLayout) LayoutInflater.from(context).inflate(
                R.layout.page_top_view, null);

        img_back = (ImageView) layout.findViewById(R.id.img_back);
        img_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) ApplicationCache.context).finish();
            }
        });

        txt_middle = (TextView) layout.findViewById(R.id.txt_middle);
        if (StringUtils.isNotBlank(title)){
            txt_middle.setText(title);
        }

        img_right = (ImageView) layout.findViewById(R.id.img_right);
        if (null != rightRes  && rightRes != 0){
            img_right.setImageResource(rightRes);
            img_right.setVisibility(View.VISIBLE);
        }

        txt_pt_right = (TextView) layout.findViewById(R.id.txt_pt_right);
        if (StringUtils.isNotBlank(rightTxt)){
            txt_pt_right.setText(rightTxt);
            txt_pt_right.setVisibility(View.VISIBLE);
        }

        addView(layout, new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
    }


    public void setRightResListener(OnClickListener listener){
        if (null != img_right){
            img_right.setOnClickListener(listener);
        }
    }

    public void setRightTxtListener(OnClickListener listener){
        if (null != txt_pt_right) {
            txt_pt_right.setOnClickListener(listener);
        }
    }

    /**
     * @param middle_text 最左边的是返回键 只适用于左边有一个返回键的
     */
    public void initTop(String middle_text) {
        txt_middle.setText(middle_text);
    }
//
//    /**
//     * @param middle_text 最左边的是返回键 只适用于左边有一个返回键的
//     */
//    public void initTop(String middle_text, String right_text, OnClickListener listener) {
//        img_back.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ((Activity) ApplicationCache.context).finish();
//            }
//        });
//        txt_middle.setText(middle_text);
//        txt_pt_right.setText(right_text);
//        txt_pt_right.setOnClickListener(listener);
//    }
//
//    /**
//     * @param middle_text 最左边的是返回键 只适用于左边有一个返回键的
//     */
//    public void initTop(String middle_text, int right_resId, OnClickListener listener) {
//        txt_middle.setText(middle_text);
//        txt_pt_right.setVisibility(View.GONE);
//        img_right.setVisibility(View.VISIBLE);
//        img_right.setBackgroundResource(right_resId);
//        img_right.setOnClickListener(listener);
//    }


}
